#include "page1.h"


page1::page1(QWidget *parent)
	: QWidget(parent)
{
	this->hasReadFile = false;

	this->setWindowIcon(QIcon("Image\\运动会.svg"));
	QPixmap pixmap("Image\\光标.svg");
	//pixmap.scaled(40, 30, Qt::KeepAspectRatio, Qt::SmoothTransformation);
	pixmap = pixmap.scaled(30, 24, Qt::KeepAspectRatio);
	QCursor cursor(pixmap);
	this->setCursor(cursor);


	myData = new MyData();
	ui.setupUi(this);

	// 初始化列表控件后，强制清除选中状态
	ui.listWidget->setCurrentRow(0);  
	ui.listWidget_2->setCurrentRow(0);
	ui.listWidget_3->setCurrentRow(0);

	ui.listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
	ui.listWidget_2->setContextMenuPolicy(Qt::CustomContextMenu);
	ui.listWidget_3->setContextMenuPolicy(Qt::CustomContextMenu);

	this->setContextMenuPolicy(Qt::NoContextMenu);
	
	this->delActionItem = menu.addAction("删除");
	delActionItem->setIcon(QIcon("image\\删除.svg"));
	menu.addSeparator();
	this->addActionItem = menu.addAction("添加");
	addActionItem->setIcon(QIcon("image\\添加.svg"));


	connect(delActionItem, &QAction::triggered,this, &page1::deleteItem);
	connect(addActionItem, &QAction::triggered,this, &page1::addItem);

	connect(ui.update, &QPushButton::clicked, this, &page1::update);
	connect(ui.admit, &QPushButton::clicked, this, &page1::admit);
	connect(ui.listWidget, &QListWidget::customContextMenuRequested,this,&page1::showContextMenu1);
	connect(ui.listWidget_2, &QListWidget::customContextMenuRequested,this,&page1::showContextMenu2);
	connect(ui.listWidget_3, &QListWidget::customContextMenuRequested,this,&page1::showContextMenu3);
	connect(ui.openFile, &QPushButton::clicked, this, &page1::openJsonFile);
	//connect(ui.del, &QPushButton::clicked, this, &page1::deleteItem);
	this->parent = dynamic_cast<SportMeeting*>(parent);
}
page1::~page1()
{
	delete myData;
}

bool page1::judgeEmpty(int n1,int n2,int n3) {
	bool empty = true;
	if (n1 <= 0) {			// 弹窗1
		message("警告", "队伍数不合法", QMessageBox::Icon::Warning, QMessageBox::Ok, this);
	}
	else if (n2 <= 0) {		// 弹窗2
		message("警告", "男子项目数不合法", QMessageBox::Icon::Warning, QMessageBox::Ok, this);
	}
	else if (n3 <= 0) {		// 弹窗3
		message("警告", "女子项目数不合法", QMessageBox::Icon::Warning, QMessageBox::Ok, this);
	}
	else {
		empty = false;
	}
	return empty;
}
void updateItemNum(int num,QListWidget* listWidget) {
	int cnt = listWidget->count();
	if (cnt > num) {
		for (int i = cnt - 1; i >= num; i--) {
			listWidget->takeItem(i);
		}
	}
	else if (cnt < num) {
		for (int i = cnt; i < num; i++) {
			QListWidgetItem* item = new QListWidgetItem("新建项目");
			item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable|Qt::ItemIsSelectable|Qt::ItemIsDragEnabled);
			listWidget->addItem(item);
		}
	}
}
void page1::setBoyItems() {
	/*this->myData->clearBoyItems();
	int num = this->myData->getItemNum1();
	for (int i = 0; i < num; i++) {
		QString name = ui.listWidget_2->item(i)->text();
		boyItem item(name);
		this->myData->addBoyItem(item);
	}*/
	int num = this->myData->getItemNum1();
	QVector<boyItem>& boyItems = this->myData->getBoyItems();
	for (int i = 0; i < num; i++) {
		QString name = ui.listWidget_2->item(i)->text();
		if (i >= boyItems.size()) {
			boyItem item(name);
			this->myData->addBoyItem(item);
		}
		else {
			boyItems[i].setName(name);
		}
	}
}
void page1::setGirlItems() {
	/*this->myData->clearGirlItems();
	int num = this->myData->getItemNum2();
	for (int i = 0; i < num; i++) {
		QString name = ui.listWidget_3->item(i)->text();
		girlItem item(name);
		this->myData->addGirlItem(item);
	}*/
	int num = this->myData->getItemNum2();
	QVector<girlItem>& girlItems = this->myData->getGirlItems();
	for (int i = 0; i < num; i++) {
		QString name = ui.listWidget_3->item(i)->text();
		if (i >= girlItems.size()) {
			girlItem item(name);
			this->myData->addGirlItem(item);
		}
		else {
			girlItems[i].setName(name);
		}
	}
}
void page1::setTeams() {
	/*this->myData->clearTeams();
	int num = this->myData->getTeamNum();
	for (int i = 0; i < num; i++) {
		QString name = ui.listWidget->item(i)->text();
		team t(name);
		this->myData->addTeamItem(t);
	}*/
	int num = this->myData->getTeamNum();
	QVector<team>& teams = this->myData->getTeams();
	for (int i = 0; i < num; i++) {
		QString name = ui.listWidget->item(i)->text();
		if (i >= teams.size()) {
			team item(name);
			this->myData->addTeamItem(item);
		}
		else {
			teams[i].setName(name);
		}

	}
}

// 槽函数开始
bool page1::update() {
	int num1 = ui.item1->text().toInt();
	int num2 = ui.item2->text().toInt();
	int num = ui.teams->text().toInt();
	if (!this->judgeEmpty(num,num1,num2)) {
		this->myData->setItemNum1(num1);
		this->myData->setItemNum2(num2);
		this->myData->setTeamNum(num);

		updateItemNum(num, ui.listWidget);
		updateItemNum(num1, ui.listWidget_2);
		updateItemNum(num2, ui.listWidget_3);

		// 初始化列表控件后，强制清除选中状态
		ui.listWidget->clearSelection();
		ui.listWidget->setCurrentRow(-1);

		ui.listWidget_2->clearSelection();
		ui.listWidget_2->setCurrentRow(-1);

		ui.listWidget_3->clearSelection();
		ui.listWidget_3->setCurrentRow(-1);

		return true;
	}
	else {
		return false;
	}
}
void page1::admit() {
	if (hasReadFile) {
		parent->pageTwo->setHasReadFile(hasReadFile);
	}
	if (this->update()&&this->judge()) {
		setBoyItems();
		setGirlItems();
		setTeams();
		// 切换到下一个页面
		parent->pageTwo->setData(myData);
		parent->pageTwo->loadData();
		setPage(parent, parent->pageTwo);
	}

}

void page1::deleteItem() {
	int index = this->curListWidget->currentRow();
	this->curListWidget->takeItem(index);
	int num;
	QString strNum;
	if (index != -1) {
		if (curListWidget == ui.listWidget) {
			num = ui.teams->text().toInt();
			strNum = QString::number(num - 1);
			ui.teams->setText(strNum);
		}
		else if (curListWidget == ui.listWidget_2) {
			num = ui.item1->text().toInt();
			strNum = QString::number(num - 1);
			ui.item1->setText(strNum);
		}
		else if (curListWidget == ui.listWidget_3) {
			num = ui.item2->text().toInt();
			strNum = QString::number(num - 1);
			ui.item2->setText(strNum);
		}
	}
	/*int index1 = -1, index2 = -1, index3 = -1;

	index1 = ui.listWidget->currentRow();
	index2 = ui.listWidget_2->currentRow();
	index3 = ui.listWidget_3->currentRow();

	ui.listWidget->takeItem(index1);			
	ui.listWidget_2->takeItem(index2);
	ui.listWidget_3->takeItem(index3);*/
}
void page1::addItem() {
	// TODO
	int index = this->curListWidget->currentRow();
	int num;
	QString strNum;
	if (index != -1) {
		QListWidgetItem* item = new QListWidgetItem("新建项目");
		item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
		this->curListWidget->insertItem(index,item);
		if (curListWidget == ui.listWidget) {
			num = ui.teams->text().toInt();
			strNum = QString::number(num + 1);
			ui.teams->setText(strNum);
		}
		else if (curListWidget == ui.listWidget_2) {
			num = ui.item1->text().toInt();
			strNum = QString::number(num + 1);
			ui.item1->setText(strNum);
		}
		else if (curListWidget == ui.listWidget_3) {
			num = ui.item2->text().toInt();
			strNum = QString::number(num + 1);
			ui.item2->setText(strNum);
		}
	}
}
// 设置右键的功能
void page1::showContextMenu1(const QPoint& pos) {
	QListWidgetItem* item = ui.listWidget->itemAt(pos);
	this->curListWidget = ui.listWidget;
	menu.exec(ui.listWidget->viewport()->mapToGlobal(pos));
}
void page1::showContextMenu2(const QPoint& pos) {
	QListWidgetItem* item = ui.listWidget_2->itemAt(pos);
	this->curListWidget = ui.listWidget_2;
	menu.exec(ui.listWidget_2->viewport()->mapToGlobal(pos));
}
void page1::showContextMenu3(const QPoint& pos) {
	QListWidgetItem* item = ui.listWidget_3->itemAt(pos);
	this->curListWidget = ui.listWidget_3;
	menu.exec(ui.listWidget_3->viewport()->mapToGlobal(pos));
}
void page1::loadDate() {
	ui.teams->setText(QString::number(this->myData->getTeamNum()));
	ui.item1->setText(QString::number(this->myData->getItemNum1()));
	ui.item2->setText(QString::number(this->myData->getItemNum2()));

	ui.listWidget->clear();
	QVector<boyItem>& boyItems = this->myData->getBoyItems();
	QVector<girlItem>& girlItems = this->myData->getGirlItems();
	QVector<team>& teams = this->myData->getTeams();
	for (int i = 0; i < teams.size(); i++) {
		QListWidgetItem* item = new QListWidgetItem(teams[i].getName());
		ui.listWidget->addItem(item);
	}
	ui.listWidget_2->clear();
	for (int i = 0; i < boyItems.size(); i++) {
		QListWidgetItem* item = new QListWidgetItem(boyItems[i].getName());
		ui.listWidget_2->addItem(item);
	}
	ui.listWidget_3->clear();
	for (int i = 0; i < girlItems.size(); i++) {
		QListWidgetItem* item = new QListWidgetItem(girlItems[i].getName());
		ui.listWidget_3->addItem(item);
	}
}
void page1::openJsonFile() {
	QString filePath = QFileDialog::getOpenFileName(
		nullptr,                // 父窗口（可为空）
		"选择运动会数据文件",   // 对话框标题
		QDir::homePath(),       // 初始目录（用户主目录）
		"JSON 文件 (*.json);;所有文件 (*.*)" // 文件过滤器
	);
	if (this->myData->readJsonDate(filePath)) {
		// LOAD THE DATA
		this->loadDate();
		this->hasReadFile = true;
	}
	else {
		//friend void message(QString title, QString text, QMessageBox::Icon, QMessageBox::Button, QWidget * parent);
		message("错误", "读取文件失败", QMessageBox::Icon::Critical, QMessageBox::Ok, this);
	}
}
bool page1::judge() {
	QSet<QString> teamName;
	QSet<QString> boyItemName;
	QSet<QString> girlItemName;
	int row = this->myData->getTeamNum();
	int column1 = this->myData->getItemNum1();
	int column2 = this->myData->getItemNum2();
	QString name;
	bool ok = true;
	for (int i = 0; i < row; i++) {
		name = this->ui.listWidget->item(i)->text();
		if (name == "") {
			message("错误", "队伍名不能为空", QMessageBox::Icon::Critical, QMessageBox::Ok, this);
			ok = false;
			return ok;
		}
		teamName.insert(name);
	}
	if (teamName.size() != row) {
		message("错误", "队伍名不可重复", QMessageBox::Icon::Critical, QMessageBox::Ok, this);
		ok = false;
		return ok;
	}
	for (int i = 0; i < column1; i++) {
		name = this->ui.listWidget_2->item(i)->text();
		if (name == "") {
			message("错误", "男生项目不能为空", QMessageBox::Icon::Critical, QMessageBox::Ok, this);
			ok = false;
			return ok;
		}
		boyItemName.insert(name);
	}
	if (boyItemName.size() != column1) {
		message("错误", "男生项目不可重复", QMessageBox::Icon::Critical, QMessageBox::Ok, this);
		ok = false;
		return ok;
	}
	for (int i = 0; i < column2; i++) {
		name = this->ui.listWidget_3->item(i)->text();
		if (name == "") {
			message("错误", "女生项目不能为空", QMessageBox::Icon::Critical, QMessageBox::Ok, this);
			ok = false;
			return ok;
		}
		girlItemName.insert(name);
	}
	if (girlItemName.size() != column2) {
		message("错误", "女生项目不可重复", QMessageBox::Icon::Critical, QMessageBox::Ok, this);
		ok = false;
		return ok;
	}
	return ok;
}